﻿using System;

namespace NDBGen.Descriptors.Columns.Tokens
{    
    public abstract class ColumnTokenDescriptorBase : ColumnDescriptorBase, IColumnTokenDescriptor
    {
        public ColumnTokenDescriptorBase() { }

        internal ColumnTokenDescriptorBase(Table table, String columnName, String tokenValue)
        {
            var column = new Column(table, columnName, DataType.None);
            this.Init(table, column, tokenValue);
        }

        protected abstract Char Token { get; }

        public virtual String TokenValue { get; protected set; }

        protected Boolean InitCalled { get; set; }

        protected virtual void Init(Table table, Column column, String tokenValue)
        {
            base.Init(table, column);

            if (tokenValue != null) this.TokenValue = tokenValue;

            this.InitCalled = true;
        }

        #region [ IColumnTokenDescriptor Members ]

        void IColumnTokenDescriptor.Init(Table table, Column column, String tokenValue)
        {
            this.Init(table, column, tokenValue);
        }

        Char ITokenDescriptor.Token { get { return this.Token; } }

        #endregion

        public override String ToString()
        {
            return this.Token.ToString();
        }

        public sealed override Boolean Equals(Object obj)
        {
            var that = obj as ColumnTokenDescriptorBase;
            if (that == null) return false;
            else
            {
                var thisType = this.GetType();
                var thatType = that.GetType();
                return thisType == thatType;
            }
        }

        public sealed override Int32 GetHashCode()
        {
            var hashCode = this.Token.GetHashCode();
            if (this.TokenValue != null) hashCode ^= this.TokenValue.GetHashCode();

            return hashCode;
        }
    }
}